###############################################################################
#                                                                             #
#    This file is part of the "ssbot" Python module.                          #
#    It is distributed under the MIT License. See LICENSE.txt for details.    #
#                                                                             #
###############################################################################

import os.path

import ssutils
import steps

class SCM(object):
    def __init__(self, url, branch, path, repo_name):
        self.url = url
        self.branch = branch
        self.path = path
        self.repo_name = repo_name

    def sync_steps(self, rev=None, remote=None):
        raise NotImplementedError()

    def patch_step(self, patch_file_name):
        return steps.Step(
                    '__simplebot_scm_step_apply_patch_%s' % self.repo_name,
                    ['patch', '-t', '-p0', '-i', patch_file_name],
                    os.path.join(self.path, self.repo_name))


class Git(SCM):
    def pull_tot_step(self, deps):
        return steps.Step(
            '__simplebot_git_step_pull_tot_%s' % self.repo_name,
            ['git', 'pull', '--rebase'],
            os.path.join(self.path, self.repo_name), deps)

#       def run(self, base_path, options=None):
#           result = steps.Step.run(self, base_path, options)
#           log_str = ssutils.run_cmd(
#               ['git', 'log', '-1', '--pretty=oneline'],
#               os.path.join(base_path, self.cwd),
#               'Unable to get the HEAD commit sha.')
#           result['data'] = 'HEAD at commit:\n%s' % log_str
#           return result

    def git_clone_step(self, base_path, remote):
        skip = False
        repo_path = os.path.join(base_path, self.path, self.repo_name)
        if remote is not None:
            if remote.path_exists(repo_path):
                skip = True
        else:
            if os.path.exists(repo_path):
                skip = True
        if skip:
            return steps.Step(
                '__simplebot_git_step_clone_repo_%s' % self.repo_name,
                ['echo', 'skipped'],
                self.path)
        else:
            return steps.Step(
                '__simplebot_git_step_clone_repo_%s' % self.repo_name,
                ['git', 'clone', self.url, self.repo_name],
                self.path)

    def sync_steps(self, base_path, rev=None, remote=None):
        repo_path = os.path.join(self.path, self.repo_name)
        s0 = self.git_clone_step(base_path, remote)
        s1 = steps.Step(
            '__simplebot_git_step_revert_local_changes_%s' % self.repo_name,
            ['git', 'checkout', '.'],
            repo_path, [s0])
        s2 = steps.Step(
            '__simplebot_git_step_clean_untracked_files_%s' % self.repo_name,
            ['git', 'clean', '-fd'],
            repo_path,
            [s1])
        s3 = steps.Step(
            '__simplebot_git_step_switch_to_master_%s' % self.repo_name,
            ['git', 'checkout', self.branch],
            repo_path,
            [s2])
        s4 = self.pull_tot_step([s3])
        step_list = [s0, s1, s2, s3, s4]
        if rev:
            s5 = steps.Step(
                '__simplebot_git_step_sync_to_rev_%s' % self.repo_name,
                ['git', 'checkout', rev],
                repo_path,
                [s4])
            step_list.append(s5)
        return step_list
